const request = require('supertest')
const app = require('../app')
const should = require('chai').should()
const expect = require('chai').expect

let myToken = ''
const user = {
  username: '8xKGHU1M',
  password: '123456'
}
const server = app.listen(9900)
// async function serve () {
//   const res = await request(server)
//     .post('/learningSpace/login')
//     .send(JSON.stringify(user))
//     .set('Content-Type', 'application/json')
//   console.log(res.body)
// }
// serve()

before(async function () {
  const res = await request(server)
    .post('/learningSpace/login')
    .send(JSON.stringify(user))
    .set('Content-Type', 'application/json')
  const { data: { token } } = res.body
  myToken = token
})

describe('测试用户模块', function () {
  it('测试获取用户信息获取是否成功', async function () {
    const res = await request(server)
      .get('/learningSpace/user/userinfo')
      .set('Authorization', 'Bearer ' + myToken)
    expect(res.body.status).to.equal(200)
  })

  it('缺失token', async function () {
    const res = await request(server)
      .get('/learningSpace/user/userinfo')
      .set('Authorization', 'Bearer ')
    expect(res.body.status).to.equal(401)
    expect(res.body.message).to.equal('token格式错误或未携带签名，签名无效')
  })

  it('获取冷启动伙伴列表', async function () {
    const res = await request(server)
      .get('/learningSpace/table/firstRecommend')
      .set('Authorization', 'Bearer ' + myToken)
    expect(res.body.status).to.equal(200)
    expect(res.body.data).to.be.a('array')
    expect(res.body.data).have.not.length(0)
  })

  it('获取学习伙伴列表', async function () {
    const res = await request(server)
      .get('/learningSpace/table/recommend')
      .set('Authorization', 'Bearer ' + myToken)
    expect(res.body.status).to.equal(200)
    expect(res.body.data).to.be.a('array')
    expect(res.body.data).have.not.length(0)
  })

  it('获取文件列表', async function () {
    const res = await request(server)
      .get('/learningSpace/user/file')
      .set('Authorization', 'Bearer ' + myToken)
    expect(res.body.status).to.equal(200)
    expect(res.body.data.list).to.be.a('array')
    expect(res.body.data.list).have.not.length(0)
  })

  it.skip('上传非同名文件', async function () {
    const res = await request(server)
      .post('/learningSpace/user/upload')
      .set('Authorization', 'Bearer ' + myToken)
      .type('multipart/form-data')
      .attach('file', './learn.rb')
    expect(res.body.status).to.equal(200)
  })

  it('上传同名文件', async function () {
    const res = await request(server)
      .post('/learningSpace/user/upload')
      .set('Authorization', 'Bearer ' + myToken)
      .type('multipart/form-data')
      .attach('file', './name.txt')
    expect(res.body.status).to.equal(403)
    expect(res.body.message).to.equal('文件名重复')
  })

  it('下载不存在的文件', async function () {
    const res = await request(server)
      .post('/learningSpace/user/download')
      .send({ name: 'admin.js' })
      .set('Authorization', 'Bearer ' + myToken)
    expect(res.body.status).to.equal(404)
    expect(res.body.message).to.equal('无此文件')
  })
})
